lib = Dir('lib')
build = Dir('build')
src = Dir('src')
inc = Dir('include')
bin = Dir('bin')

prefix = Dir(ARGUMENTS.get('prefix', '.'))
prefix_lib = prefix.Dir('lib')
prefix_inc = prefix.Dir('include')
prefix_bin = prefix.Dir('bin')


import os
cpath = os.environ.get('CPATH', "")
ld_library_path = ""#os.environ.get('LD_LIBRARY_PATH', "")
cpp_paths = cpath.split(':')+[prefix_inc, inc]
lib_paths = ld_library_path.split(':')+[lib, prefix_lib]

ccflags = ['-Wall', '-Werror']

if 'debug' in COMMAND_LINE_TARGETS:
	ccflags += ['-g']

BuildDir(build, src, duplicate=0)

agent_sources = [build.File(source) for source in """
agent_codec.c
""".split()]
env_sources = [build.File(source) for source in """
env_codec.c
""".split()]

env = Environment(tools = ["default", "tools"], toolpath = '.',
                  CPPPATH=cpp_paths, CCFLAGS=ccflags, LIBPATH=lib_paths)

shared_lib_suffix = ".so"
if os.uname()[0] == "Darwin":
	shared_lib_suffix = ".dylib"
	env.Append(SHLINKFLAGS='-undefined dynamic_lookup')
	
config = Configure(env)
config.CheckHeader('rlglue/Agent_common.h')
config.CheckHeader('rlglue/Environment_common.h')
config.CheckHeader('rlglue/utils/C/RLStruct_util.h')
config.CheckHeader('rlglue/network/RL_network.h')
env = config.Finish()

librlgnmagent = env.SharedLibrary(lib.File('librlgnmagent'+shared_lib_suffix), agent_sources)
Alias('librlgnmagent', librlgnmagent)
librlgnmenv = env.SharedLibrary(lib.File('librlgnmenv'+shared_lib_suffix), env_sources)
Alias('librlgnmenv', librlgnmenv)

if prefix != Dir('.'):
	env.Alias('install', env.InstallPerm(prefix_lib, librlgnmagent, 0664))
	env.Alias('install', env.InstallPerm(prefix_lib, librlgnmenv, 0664))
	for header in ['rlgnmagent.h', 'rlgnmenv.h']:
		env.Alias('install', env.InstallPerm(prefix_inc, inc.Entry(header), 0664))

Default(librlgnmagent)
Default(librlgnmenv)

env.Alias('debug', librlgnmagent)
env.Alias('debug', librlgnmenv)

dist_files = [File(x) for x in """
COPYING COPYING.LESSER SConstruct tools.py
""".split()]
dist_files += [inc.Entry(header) for header in ['rlgnmagent.h', 'rlgnmenv.h']]

dist_files += [src.Entry(agent_source) for agent_source in agent_sources] 
dist_files += [src.Entry(env_source) for env_source in env_sources] 

env.Alias('dist', env.DistTar('rlgnm.tar.gz', dist_files))
